
//
//  LXEditUserSignViewController.m
//  cat
//
//  Created by imera on 16/3/16.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXEditUserSignViewController.h"
#import "LXNavBarView.h"
#import "AFNetworking.h"
#import "MBProgressHUD+MJ.h"

@interface LXEditUserSignViewController() <LXNavBarViewDelegate>

@property (nonatomic, weak) LXNavBarView *navBarView;

/** 输入框 */
@property (nonatomic, weak) UITextView *textView;
@end

@implementation LXEditUserSignViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = MainBackgroundColor;
    
    [self initSubViews];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:nil];
}

#pragma mark -  监听文本框变化
- (void)textChange
{
    self.navBarView.rightBtn.enabled = YES;
}

- (void)initSubViews
{
    /** 导航条 */
    LXNavBarView *navBarView = [[LXNavBarView alloc]init];
    navBarView.leftTitle = @"修改个性签名";
    navBarView.rightTitle = @"保存";
    navBarView.myDelegate = self;
    navBarView.rightBtn.enabled = NO;
    navBarView.frame = CGRectMake(0, 20, self.view.frame.size.width, LXNavBarHeight);
    [self.view addSubview:navBarView];
    self.navBarView = navBarView;
    
    
    /** 文本输入框 */
    UITextView *textView = [[UITextView alloc]init];
    textView.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:textView];
    self.textView = textView;
    
    CGFloat textViewX = 10;
    CGFloat textViewY = 20 + LXNavBarHeight + 10;
    CGFloat textViewW = self.view.frame.size.width - textViewX * 2;
    CGFloat textViewH = 70;
    textView.frame = CGRectMake(textViewX, textViewY, textViewW, textViewH);
}

#pragma mark - 自定义导航栏代理
- (void)popCurrentViewController:(LXNavBarView *)navBarView
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightBtnClicked:(LXNavBarView *)navBarView
{
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    
    [mgr POST:@"http://cat666.com/cat666-interface/index.php/index/alterSignature" parameters:@{@"userid":@(self.user.ID), @"signature":self.textView.text} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dict = responseObject;
        if([dict[@"successed"] integerValue] == 1){
            
            [MBProgressHUD showSuccess:@"修改个性签名成功"];
            
            self.user.info = self.textView.text;
            
            [[NSNotificationCenter defaultCenter]postNotificationName:@"userInfoChange" object:nil];
            
        }else{
            [MBProgressHUD showSuccess:@"修改个性签名失败"];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD showSuccess:@"修改个性签名失败"];
    }];
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
